/*
 * PositionObject.cpp
 *
 *  Created on: 02.10.2012
 *      Author: caffeine
 */

#include "PositionObject.h"

namespace irr{
namespace object{

PositionObject::PositionObject() {
	position.set(0,0,0);
	position_lock=irr::thread::createRecursiveMutex();
}

PositionObject::PositionObject(irr::core::vector3df pos){
	position=pos;
	position_lock=irr::thread::createRecursiveMutex();
}

PositionObject::PositionObject(irr::f32 X,irr::f32 Y,irr::f32 Z){
	position.set(X,Y,Z);
	position_lock=irr::thread::createRecursiveMutex();
}

irr::core::vector3df PositionObject::getPosition(){
	position_lock->lock();
	irr::core::vector3df res=position;
	position_lock->unlock();
	return res;
}
void PositionObject::setPosition(irr::core::vector3df pos){
	position_lock->lock();
	position=pos;
	position_lock->unlock();
}

void PositionObject::setPosition(irr::f32 X,irr::f32 Y,irr::f32 Z){
	position_lock->lock();
	position.set(X,Y,Z);
	position_lock->unlock();
}

void PositionObject::setPositionX(irr::f32 X){
	position_lock->lock();
	position.X=X;
	position_lock->unlock();
}
void PositionObject::setPositionY(irr::f32 Y){
	position_lock->lock();
	position.Y=Y;
	position_lock->unlock();
}
void PositionObject::setPositionZ(irr::f32 Z){
	position_lock->lock();
	position.Z=Z;
	position_lock->unlock();
}

irr::f32 PositionObject::getPositionX(){
	position_lock->lock();
	return position.X;
	position_lock->unlock();
}
irr::f32 PositionObject::getPositionY(){
	position_lock->lock();
	return position.Y;
	position_lock->unlock();
}
irr::f32 PositionObject::getPositionZ(){
	position_lock->lock();
	return position.Z;
	position_lock->unlock();
}

PositionObject::~PositionObject() {
	position_lock->wait();
	position_lock->drop();
}

irr::core::stringc PositionObject::getSerializationNodeName(){
	return "object::position";
}

void PositionObject::getSerialized(irr::xml::IXmlNode*node){
	node->setAttVec3F("position",getPosition());
}

irr::xml::TagResult PositionObject::setSerialized(irr::xml::IXmlNode*node){
	setPosition(node->getAttVec3F("position"));
}

irr::f32 PositionObject::getDistanceSQ(irr::core::vector3df point){
	getPosition().getDistanceFromSQ(point);
}

irr::f32 PositionObject::getDistanceSQ(PositionObject*object){
	return getDistanceSQ(object->getPosition());
}

irr::f32 PositionObject::getDistance(PositionObject*object){
	return getPosition().getDistanceFrom(object->getPosition());
}

}//namespace object
}//namespace irr
